package com.dingxin.portal.common.dao;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.support.incrementer.AbstractSequenceMaxValueIncrementer;

public class Incrementer implements ApplicationContextAware,InitializingBean{
	private ApplicationContext applicationContext;
	private String sessionFactory;
	private SessionFactoryBean sessionFactoryBean;

	public void setSessionFactory(String sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
		this.applicationContext=applicationContext;
	}
	public long nextValueFor(String incrementerName){
		AbstractSequenceMaxValueIncrementer incrementer=sessionFactoryBean.getIncrementer(incrementerName);
		return incrementer.nextLongValue();
	}
	public void afterPropertiesSet() {
		sessionFactoryBean =(SessionFactoryBean)applicationContext.getBean("&"+sessionFactory);
	}
}
